﻿using AntdUI;
using AntdUI.In;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using Microsoft.IdentityModel.Tokens;
using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using ScottPlot.MultiplotLayouts;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace BST250301.Views
{
    public partial class ProjectEdit : UserControl
    {
        private AntdUI.Window window;
        //private Target tag;
        private ProjectItem _item;
        private List<RecordData> _listRecord = new List<RecordData>();
        public bool submit;
        //string currentArea = string.Empty;
        public ProjectEdit(AntdUI.Window win, ProjectItem item, bool isedit = true)
        {
            //tag = Target.GetInstance(this);
            this.window = win;
            if (item != null)
                this._item = item;
            if (this._item.SetPointCurrentKP == null)
                this._item.SetPointCurrentKP = new StepTest();
            InitializeComponent();
            GenerateCheckboxMatrix();
            selectCFState.SelectedIndexChanged -= selectCFState_SelectedIndexChanged;
            selectCRState.SelectedIndexChanged -= selectCRState_SelectedIndexChanged;
            InitData();
            this.panelKP.Visible = false;

            if (!isedit)
            {
                this.buttonAdd.Enabled = false;
            }
            BindEventHandler();
            flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
            flowLayoutPanel1.WrapContents = true;
            flowLayoutPanel1.AutoScroll = true;
        }

        private void BindEventHandler()
        {
            selectCFState.SelectedIndexChanged += selectCFState_SelectedIndexChanged;
            selectCRState.SelectedIndexChanged += selectCRState_SelectedIndexChanged;

        }
        private void InitData()
        {
            //清理下拉控件
            selectMasterGear.Items.Clear();
            selectSlaveGear.Items.Clear();
            selectCFState.Items.Clear();
            selectTemperatureOil.Items.Clear();
            selectCondition.Items.Clear();
            selectPTOGear.Items.Clear();

            //初始化挡位
            var ret = Enum.GetValues(typeof(GearM));
            foreach (var item in ret)
            {
                selectMasterGear.Items.Add(item);
                if (this._item != null && this._item.MasterGearPosition == (GearM)item)
                    selectMasterGear.SelectedIndex = (int)item;
            }
            ret = Enum.GetValues(typeof(GearS));
            foreach (var item in ret)
            {
                selectSlaveGear.Items.Add(item);
                if (this._item != null && this._item.SlaveGearPosition == (GearS)item)
                    selectSlaveGear.SelectedIndex = (int)item;
            }
            ret = Enum.GetValues(typeof(GearPTO));
            foreach (var item in ret)
            {
                selectPTOGear.Items.Add(item);
                if (this._item != null && this._item.PTOGearPosition == (GearPTO)item)
                    selectPTOGear.SelectedIndex = (int)item;
            }
            //初始化离合
            ret = Enum.GetValues(typeof(ClutchStatus));
            foreach (var item in ret)
            {
                selectCFState.Items.Add(item);
                if (this._item != null && this._item.CFStatus == (ClutchStatus)item)
                    selectCFState.SelectedIndex = (int)item;

                selectCRState.Items.Add(item);
                if (this._item != null && this._item.CRStatus == (ClutchStatus)item)
                    selectCRState.SelectedIndex = (int)item;

                selectCPTOState.Items.Add(item);
                if (this._item != null && this._item.CPTOStatus == (ClutchStatus)item)
                    selectCPTOState.SelectedIndex = (int)item;
            }

            //油温
            ret = Enum.GetValues(typeof(TOil));
            foreach (var item in ret)
            {
                selectTemperatureOil.Items.Add(item);
                if (this._item != null && this._item.TemperatureOil == (TOil)item)
                    selectTemperatureOil.SelectedIndex = (int)item;
            }
            //确认方式
            ret = Enum.GetValues(typeof(ConfWay));
            foreach (var item in ret)
            {
                selectCondition.Items.Add(item);
                if (this._item != null && this._item.ConfirmWay == (ConfWay)item)
                    selectCondition.SelectedIndex = (int)item;
            }

            //项目名称
            inputName.Text = this._item.Name;
            inputDesc.Text = this._item.Description;
            inputNumberTestDuration.Value = this._item.TestDuration;
            inputNumberTimes.Value = this._item.Times;
            //负载速度
            inputNumberTorque.Value = Convert.ToDecimal(this._item.LoadTorque);
            inputNumberSpeed.Value = this._item.RotateSpeed.ValueStart;
            inputNumberSpeedTo.Value = this._item.RotateSpeed.ValueEnd;
            inputNumberSpeedPerStep.Value = this._item.RotateSpeed.StepValue;
            inputNumberSpeedPerSecond.Value = this._item.RotateSpeed.StepKeep;
            if (this._item.RotateSpeed.StepValue != 0)
                this.selectSpeed.SelectedIndex = 1;
            else
                this.selectSpeed.SelectedIndex = 0;

            //CF电流
            inputNumberCFSetPointCurrentStart.Value = this._item.CFSetPointCurrent.ValueStart;
            inputNumberCFSetPointCurrentEnd.Value = this._item.CFSetPointCurrent.ValueEnd;
            inputNumberCFSetPointCurrentStep.Value = this._item.CFSetPointCurrent.StepValue;
            inputNumberCFSetPointCurrentKeep.Value = this._item.CFSetPointCurrent.StepKeep;
            if (this._item.CFSetPointCurrent.StepValue != 0)
                this.selectCFSetPointCurrent.SelectedIndex = 1;
            else
                this.selectCFSetPointCurrent.SelectedIndex = 0;


            if (this._item.CFSetPointCurrent.SPCMode == 1)
            {
                this.labelCFStep.Text = "阶跃梯度";
                this.labelCFStepUnit.Text = "mA";
                this.labelCFStepKeep.Text = "阶跃步长";
                this.selectCFSetPointCurrent.SelectedIndex = 1;
            }
            else if (this._item.CFSetPointCurrent.SPCMode == 2)//脉冲方式
            {
                this.labelCFStep.Text = "起始时长";
                this.labelCFStepUnit.Text = "S";
                this.labelCFStepKeep.Text = "峰值时长";
                this.inputNumberCFPulse.Value = this._item.CFSetPointCurrent.Repeat;//循环次数
                this.selectCFSetPointCurrent.SelectedIndex = 2;
            }
            else if (this._item.CFSetPointCurrent.SPCMode == 3)//升阶KP
            {
                this.labelCFStep.Text = "阶跃梯度";
                this.labelCFStepUnit.Text = "mA";
                this.labelCFStepKeep.Text = "阶跃步长";
                this.selectCFSetPointCurrent.SelectedIndex = 3;
                this.inputNumberKPStartStep.Value =this._item.SetPointCurrentKP.KPStartKeep ;//KP起步保持时间
                this.inputNumberKPEndStep.Value = this._item.SetPointCurrentKP.KPEndKeep;//KP峰值保持时间
                this.inputNumberKPMaxTransSpeed.Value = (decimal)this._item.SetPointCurrentKP.KPMaxOutSpeed;//KP最大输出转速
            }

            //CR电流
            inputNumberCRSetPointCurrentStart.Value = this._item.CRSetPointCurrent.ValueStart;
            inputNumberCRSetPointCurrentEnd.Value = this._item.CRSetPointCurrent.ValueEnd;
            inputNumberCRSetPointCurrentStep.Value = this._item.CRSetPointCurrent.StepValue;
            inputNumberCRSetPointCurrentKeep.Value = this._item.CRSetPointCurrent.StepKeep;
            if (this._item.CRSetPointCurrent.StepValue != 0)
                this.selectCRSetPointCurrent.SelectedIndex = 1;
            else
                this.selectCRSetPointCurrent.SelectedIndex = 0;
            if (this._item.CRSetPointCurrent.SPCMode == 1)
            {
                this.labelCRStep.Text = "阶跃梯度";
                this.labelCRStepUnit.Text = "mA";
                this.labelCRStepKeep.Text = "阶跃步长";
                this.selectCRSetPointCurrent.SelectedIndex = 1;
            }
            else if (this._item.CRSetPointCurrent.SPCMode == 2)//脉冲方式
            {
                this.labelCRStep.Text = "起始时长";
                this.labelCRStepUnit.Text = "S";
                this.labelCRStepKeep.Text = "峰值时长";
                this.inputNumberCRPulse.Value = this._item.CRSetPointCurrent.Repeat;//循环次数
                this.selectCRSetPointCurrent.SelectedIndex = 2;
            }
            else if (this._item.CRSetPointCurrent.SPCMode == 3)//升阶KP
            {
                this.labelCRStep.Text = "阶跃梯度";
                this.labelCRStepUnit.Text = "mA";
                this.labelCRStepKeep.Text = "阶跃步长";
                this.selectCRSetPointCurrent.SelectedIndex = 3;
                this.inputNumberKPStartStep.Value = this._item.SetPointCurrentKP.KPStartKeep;//KP起步保持时间
                this.inputNumberKPEndStep.Value = this._item.SetPointCurrentKP.KPEndKeep;//KP峰值保持时间
                this.inputNumberKPMaxTransSpeed.Value = (decimal)this._item.SetPointCurrentKP.KPMaxOutSpeed;//KP最大输出转速
            }
            //CPTO电流
            inputNumberCPTOSetPointCurrentStart.Value = this._item.CPTOSetPointCurrent.ValueStart;
            inputNumberCPTOSetPointCurrentEnd.Value = this._item.CPTOSetPointCurrent.ValueEnd;
            inputNumberCPTOSetPointCurrentStep.Value = this._item.CPTOSetPointCurrent.StepValue;
            inputNumberCPTOSetPointCurrentKeep.Value = this._item.CPTOSetPointCurrent.StepKeep;
            if (this._item.CPTOSetPointCurrent.StepValue != 0)
                this.selectCPTOSetPointCurrent.SelectedIndex = 1;
            else
                this.selectCPTOSetPointCurrent.SelectedIndex = 0;

            if (this._item.CPTOSetPointCurrent.SPCMode == 1)
            {
                this.labelPTOStep.Text = "阶跃梯度";
                this.labelPTOStepUnit.Text = "mA";
                this.labelPTOStepKeep.Text = "阶跃步长";
                this.selectCPTOSetPointCurrent.SelectedIndex = 1;
            }
            else if (this._item.CPTOSetPointCurrent.SPCMode == 2)//脉冲方式
            {
                this.labelPTOStep.Text = "起始时长";
                this.labelPTOStepUnit.Text = "S";
                this.labelPTOStepKeep.Text = "峰值时长";
                this.inputNumberPTOPulse.Value = this._item.CPTOSetPointCurrent.Repeat;//循环次数
                this.selectCPTOSetPointCurrent.SelectedIndex = 2;
            }
            else if (this._item.CPTOSetPointCurrent.SPCMode == 3)//升阶KP
            {
                this.labelPTOStep.Text = "阶跃梯度";
                this.labelPTOStepUnit.Text = "mA";
                this.labelPTOStepKeep.Text = "阶跃步长";
                this.selectCPTOSetPointCurrent.SelectedIndex = 3;
                this.inputNumberKPStartStep.Value = this._item.SetPointCurrentKP.KPStartKeep;//KP起步保持时间
                this.inputNumberKPEndStep.Value = this._item.SetPointCurrentKP.KPEndKeep;//KP峰值保持时间
                this.inputNumberKPMaxTransSpeed.Value = (decimal)this._item.SetPointCurrentKP.KPMaxOutSpeed;//KP最大输出转速
            }
            //记录数据
            for (int i = 0; i < _listRecord.Count; i++)
            {
                if (_item.ListRecordData != null)
                    foreach (var item in _item.ListRecordData)
                    {
                        if (item == _listRecord[i].Name)
                        {
                            var checkbox = Controls.Find($"Checkbox_{_listRecord[i].ID}", true).FirstOrDefault() as System.Windows.Forms.CheckBox;
                            if (checkbox != null)
                            {
                                checkbox.Checked = true;
                            }
                            break;
                        }
                    }

            }
            //if (this._item != null)
            //{
            //    inputName.Text = this._item.Name;
            //    inputDesc.Text = this._item.Description;
            //    selectMasterGear.Text = this._item.MasterGearPosition.ToString();
            //    selectSlaveGear.Text = this._item.SlaveGearPosition.ToString();
            //    selectClutchState.Text = this._item.ClutchState.ToString();
            //    inputNumber1.Value = this._item.RotateSeeped;
            //    selectTemperatureOil.Text = this._item.TemperatureOil.ToString();
            //    selectCondition.Text = this._item.ConfirmWay == ConfWay.自动确认 ? "自动确认" : "手动确认";
            //}
            this.groupBoxCF.Enabled = false;
            this.groupBoxCR.Enabled = false;
            this.groupBoxCPTO.Enabled = false;
        }
        private void GenerateCheckboxMatrix()
        {
            _listRecord = DB.GetRecordData();
            for (int i = 0; i < _listRecord.Count; i++)
            {
                var checkbox = new System.Windows.Forms.CheckBox
                {
                    Name = $"Checkbox_{_listRecord[i].ID}",
                    Text = $"{_listRecord[i].Name}",  // 文本标签，例如 "CR 选项 1"
                    Size = new Size(300, 32),  // 固定大小
                    Margin = new Padding(5),
                    Tag = 100 + i  // 存储索引，便于事件中使用
                };
                // 添加选中事件（示例：打印选中状态）
                checkbox.CheckedChanged += (sender, e) =>
                {
                    var cb = sender as System.Windows.Forms.CheckBox;
                    Console.WriteLine($"Checkbox {cb.Tag} 选中: {cb.Checked}");
                };

                flowLayoutPanel1.Controls.Add(checkbox);
            }
            flowLayoutPanel1.PerformLayout();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            bool isnew = false;
            DialogResult confirmResult = MessageBox.Show("确认修改记录", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (this._item == null)
            {
                isnew = true;
                this._item = new ProjectItem();
                this._item.RotateSpeed = new StepTest();
                this._item.CRSetPointCurrent = new StepTest();
                this._item.CFSetPointCurrent = new StepTest();
                this._item.CPTOSetPointCurrent = new StepTest();

                this._item.Code = Guid.NewGuid().ToString("N").Substring(0, 16);
                confirmResult = MessageBox.Show("确认增加记录", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }
            if (confirmResult != DialogResult.Yes)
                return;
            try
            {
                inputName.Status = AntdUI.TType.None;

                if (String.IsNullOrEmpty(inputName.Text))
                {
                    inputName.Status = AntdUI.TType.Error;
                    AntdUI.Message.warn(window, "名称不能为空！", autoClose: 3);
                    return;
                }

                this._item.Name = inputName.Text;
                this._item.ConfirmWay = selectCondition.Text == "自动确认" ? ConfWay.自动确认 : ConfWay.手动确认;
                this._item.Description = inputDesc.Text;
                this._item.TestDuration = (int)inputNumberTestDuration.Value;
                this._item.Times = (int)inputNumberTimes.Value;

                this._item.TemperatureOil = (TOil)selectTemperatureOil.SelectedIndex;
                this._item.SlaveGearPosition = (GearS)selectSlaveGear.SelectedIndex;
                this._item.MasterGearPosition = (GearM)selectMasterGear.SelectedIndex;
                this._item.PTOGearPosition = (GearPTO)selectPTOGear.SelectedIndex;

                this._item.CFStatus = (ClutchStatus)selectCFState.SelectedIndex;
                this._item.CRStatus = (ClutchStatus)selectCRState.SelectedIndex;
                this._item.CPTOStatus = (ClutchStatus)selectCPTOState.SelectedIndex;

                if (this._item.CFStatus == ClutchStatus.吸合 && this._item.CRStatus == ClutchStatus.吸合)
                {
                    MessageBox.Show("CR与CF测试不可以同时存在！");
                    return;
                }

                this._item.LoadTorque = (int)inputNumberTorque.Value;

                this._item.RotateSpeed = new StepTest();
                this._item.RotateSpeed.ValueStart = (int)inputNumberSpeed.Value;
                if (this.selectSpeed.SelectedIndex == 1)
                {
                    this._item.RotateSpeed.IsStep = true;
                    this._item.RotateSpeed.ValueEnd = (int)inputNumberSpeedTo.Value;
                    this._item.RotateSpeed.StepValue = (int)inputNumberSpeedPerStep.Value;
                    this._item.RotateSpeed.StepKeep = (int)inputNumberSpeedPerSecond.Value;
                    if (this._item.RotateSpeed.ValueEnd == this._item.RotateSpeed.ValueStart)
                    {
                        inputName.Status = AntdUI.TType.Error;
                        AntdUI.Message.warn(window, "起始值不能与峰值相等！", autoClose: 3);
                        MessageBox.Show("起始值不能与峰值相等！");
                        return;
                    }
                    if (this._item.RotateSpeed.StepKeep == 0)
                    {
                        inputName.Status = AntdUI.TType.Error;
                        AntdUI.Message.warn(window, "阶跃时长不能为0", autoClose: 3);
                        MessageBox.Show("阶跃时长不能为0");
                        return;
                    }
                    if (this._item.RotateSpeed.StepValue == 0)
                    {
                        inputName.Status = AntdUI.TType.Error;
                        AntdUI.Message.warn(window, "阶跃值不能为0", autoClose: 3);
                        MessageBox.Show("阶跃值不能为0");
                        return;
                    }
                    if (this._item.RotateSpeed.ValueStart > this._item.RotateSpeed.ValueEnd)
                    {
                        if (this._item.RotateSpeed.StepValue >= 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃值应为负值", autoClose: 3);
                            MessageBox.Show("阶跃值应为负值");
                            return;
                        }
                    }
                }
                this._item.CFSetPointCurrent = new StepTest();
                if (this._item.CFStatus == ClutchStatus.电流控制)
                {
                    this._item.CFSetPointCurrent.SPCMode = 0;
                    this._item.CFSetPointCurrent.ValueStart = (int)inputNumberCFSetPointCurrentStart.Value;
                    if (this.selectCFSetPointCurrent.SelectedIndex == 1)
                    {
                        this._item.CFSetPointCurrent.SPCMode = 1;
                        this._item.CFSetPointCurrent.IsStep = true;
                        this._item.CFSetPointCurrent.ValueEnd = (int)inputNumberCFSetPointCurrentEnd.Value;
                        this._item.CFSetPointCurrent.StepValue = (int)inputNumberCFSetPointCurrentStep.Value;
                        this._item.CFSetPointCurrent.StepKeep = (int)inputNumberCFSetPointCurrentKeep.Value;
                        if (this._item.CFSetPointCurrent.ValueEnd == this._item.CFSetPointCurrent.ValueStart)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "起始值不能与峰值相等！", autoClose: 3);
                            MessageBox.Show("起始值不能与峰值相等！");
                            return;
                        }
                        if (this._item.CFSetPointCurrent.StepKeep == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃时长不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃时长不能为0");
                            return;
                        }
                        if (this._item.CFSetPointCurrent.StepValue == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃值不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃值不能为0");
                            return;
                        }
                        if (this._item.CFSetPointCurrent.ValueStart > this._item.CFSetPointCurrent.ValueEnd)
                        {
                            if (this._item.CFSetPointCurrent.StepValue >= 0)
                            {
                                inputName.Status = AntdUI.TType.Error;
                                AntdUI.Message.warn(window, "阶跃值应为负值", autoClose: 3);
                                MessageBox.Show("阶跃值应为负值");
                                return;
                            }
                        }
                        if (this._item.CRSetPointCurrent.ValueEnd > 0 && this._item.CFSetPointCurrent.ValueEnd > 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "CR与CF测试不可以同时存在！", autoClose: 3);
                            MessageBox.Show("CR与CF测试不可以同时存在！");
                            return;
                        }
                    }
                    else if (this.selectCFSetPointCurrent.SelectedIndex == 2)//脉冲方式
                    {
                        this._item.CFSetPointCurrent.SPCMode = 2;
                        this._item.CFSetPointCurrent.IsStep = false;
                        this._item.CFSetPointCurrent.ValueEnd = (int)inputNumberCFSetPointCurrentEnd.Value;
                        this._item.CFSetPointCurrent.StepValue = (int)inputNumberCFSetPointCurrentStep.Value;//起始脉冲时长
                        this._item.CFSetPointCurrent.StepKeep = (int)inputNumberCFSetPointCurrentKeep.Value;//峰值脉冲时长
                        this._item.CFSetPointCurrent.Repeat = (int)inputNumberCFPulse.Value;//循环次数
                    }
                    else if (this.selectCFSetPointCurrent.SelectedIndex == 3)//KP方式
                    {
                        this._item.CFSetPointCurrent.SPCMode = 3;
                        this._item.CFSetPointCurrent.ValueEnd = (int)inputNumberCFSetPointCurrentEnd.Value;
                        this._item.CFSetPointCurrent.StepValue = (int)inputNumberCFSetPointCurrentStep.Value;
                        this._item.CFSetPointCurrent.StepKeep = (int)inputNumberCFSetPointCurrentKeep.Value;

                        this._item.SetPointCurrentKP.KPStartKeep = (int)inputNumberKPStartStep.Value;//KP起步保持时间
                        this._item.SetPointCurrentKP.KPEndKeep = (int)inputNumberKPEndStep.Value;//KP峰值保持时间
                        this._item.SetPointCurrentKP.KPMaxOutSpeed = (float)inputNumberKPMaxTransSpeed.Value;//KP最大输出转速
                    }
                    else
                    {
                        this._item.CFSetPointCurrent.IsStep = false;
                        this._item.CFSetPointCurrent.ValueEnd = 0;
                        this._item.CFSetPointCurrent.StepValue = 0;
                        this._item.CFSetPointCurrent.StepKeep = 0;
                    }
                }
                else
                {
                    this._item.CFSetPointCurrent.IsStep = false;
                    this._item.CFSetPointCurrent.ValueStart = 0;
                    this._item.CFSetPointCurrent.ValueEnd = 0;
                    this._item.CFSetPointCurrent.StepValue = 0;
                    this._item.CFSetPointCurrent.StepKeep = 0;
                }

                this._item.CRSetPointCurrent = new StepTest();
                if (this._item.CRStatus == ClutchStatus.电流控制)
                {
                    this._item.CRSetPointCurrent.SPCMode = 0;

                    this._item.CRSetPointCurrent.ValueStart = (int)inputNumberCRSetPointCurrentStart.Value;
                    if (this.selectCRSetPointCurrent.SelectedIndex == 1)
                    {
                        this._item.CRSetPointCurrent.SPCMode = 1;

                        this._item.CRSetPointCurrent.IsStep = true;

                        this._item.CRSetPointCurrent.ValueEnd = (int)inputNumberCRSetPointCurrentEnd.Value;
                        this._item.CRSetPointCurrent.StepValue = (int)inputNumberCRSetPointCurrentStep.Value;
                        this._item.CRSetPointCurrent.StepKeep = (int)inputNumberCRSetPointCurrentKeep.Value;
                        if (this._item.CRSetPointCurrent.ValueEnd == this._item.CRSetPointCurrent.ValueStart)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "起始值不能与峰值相等！", autoClose: 3);
                            MessageBox.Show("起始值不能与峰值相等！");
                            return;
                        }
                        if (this._item.CRSetPointCurrent.StepKeep == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃时长不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃时长不能为0");
                            return;
                        }
                        if (this._item.CRSetPointCurrent.StepValue == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃值不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃值不能为0");
                            return;
                        }
                        if (this._item.CRSetPointCurrent.ValueStart > this._item.CRSetPointCurrent.ValueEnd)
                        {
                            if (this._item.CRSetPointCurrent.StepValue >= 0)
                            {
                                inputName.Status = AntdUI.TType.Error;
                                AntdUI.Message.warn(window, "阶跃值应为负值", autoClose: 3);
                                MessageBox.Show("阶跃值应为负值");
                                return;
                            }
                        }
                        //if (this._item.CRSetPointCurrent.ValueEnd > 0 && this._item.CFSetPointCurrent.ValueEnd > 0)
                        //{
                        //    inputName.Status = AntdUI.TType.Error;
                        //    AntdUI.Message.warn(window, "CR与CF测试不可以同时存在！", autoClose: 3);
                        //    MessageBox.Show("CR与CF测试不可以同时存在！");
                        //    return;
                        //}
                    }
                    else if (this.selectCRSetPointCurrent.SelectedIndex == 2)//脉冲方式
                    {
                        this._item.CRSetPointCurrent.SPCMode = 2;
                        this._item.CRSetPointCurrent.IsStep = false;
                        this._item.CRSetPointCurrent.ValueEnd = (int)inputNumberCRSetPointCurrentEnd.Value;
                        this._item.CRSetPointCurrent.StepValue = (int)inputNumberCRSetPointCurrentStep.Value;//起始脉冲时长
                        this._item.CRSetPointCurrent.StepKeep = (int)inputNumberCRSetPointCurrentKeep.Value;//峰值脉冲时长
                        this._item.CRSetPointCurrent.Repeat = (int)inputNumberCRPulse.Value;//循环次数
                    }
                    else if (this.selectCRSetPointCurrent.SelectedIndex == 3)//KP方式
                    {
                        this._item.CRSetPointCurrent.SPCMode = 3;
                        this._item.CRSetPointCurrent.IsStep = true;
                        this._item.CRSetPointCurrent.ValueEnd = (int)inputNumberCRSetPointCurrentEnd.Value;
                        this._item.CRSetPointCurrent.StepValue = (int)inputNumberCRSetPointCurrentStep.Value;
                        this._item.CRSetPointCurrent.StepKeep = (int)inputNumberCRSetPointCurrentKeep.Value;

                        this._item.SetPointCurrentKP.KPStartKeep = (int)inputNumberKPStartStep.Value;//KP起步保持时间
                        this._item.SetPointCurrentKP.KPEndKeep = (int)inputNumberKPEndStep.Value;//KP峰值保持时间
                        this._item.SetPointCurrentKP.KPMaxOutSpeed = (float)inputNumberKPMaxTransSpeed.Value;//KP最大输出转速
                    }
                    else
                    {
                        this._item.CRSetPointCurrent.IsStep = false;
                        this._item.CRSetPointCurrent.ValueEnd = 0;
                        this._item.CRSetPointCurrent.StepValue = 0;
                        this._item.CRSetPointCurrent.StepKeep = 0;
                    }
                }
                else
                {
                    this._item.CRSetPointCurrent.IsStep = false;
                    this._item.CRSetPointCurrent.ValueStart = 0;
                    this._item.CRSetPointCurrent.ValueEnd = 0;
                    this._item.CRSetPointCurrent.StepValue = 0;
                    this._item.CRSetPointCurrent.StepKeep = 0;
                }

                if (groupBoxCF.Enabled && groupBoxCR.Enabled)
                {
                    MessageBox.Show("CR与CF测试不可以同时存在！");
                    return;
                }

                this._item.CPTOSetPointCurrent = new StepTest();
                if (this._item.CPTOStatus == ClutchStatus.电流控制)
                {
                    this._item.CPTOSetPointCurrent.SPCMode = 0;
                    this._item.CPTOSetPointCurrent.ValueStart = (int)inputNumberCPTOSetPointCurrentStart.Value;
                    if (this.selectCPTOSetPointCurrent.SelectedIndex == 1)
                    {
                        this._item.CPTOSetPointCurrent.SPCMode = 1;
                        this._item.CPTOSetPointCurrent.IsStep = true;

                        this._item.CPTOSetPointCurrent.ValueEnd = (int)inputNumberCPTOSetPointCurrentEnd.Value;
                        this._item.CPTOSetPointCurrent.StepValue = (int)inputNumberCPTOSetPointCurrentStep.Value;
                        this._item.CPTOSetPointCurrent.StepKeep = (int)inputNumberCPTOSetPointCurrentKeep.Value;
                        if (this._item.CPTOSetPointCurrent.ValueEnd == this._item.CPTOSetPointCurrent.ValueStart)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "起始值不能与峰值相等！", autoClose: 3);
                            MessageBox.Show("起始值不能与峰值相等！");
                            return;
                        }
                        if (this._item.CPTOSetPointCurrent.StepKeep == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃时长不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃时长不能为0");
                            return;
                        }
                        if (this._item.CPTOSetPointCurrent.StepValue == 0)
                        {
                            inputName.Status = AntdUI.TType.Error;
                            AntdUI.Message.warn(window, "阶跃值不能为0！", autoClose: 3);
                            MessageBox.Show("阶跃值不能为0");
                            return;
                        }
                        if (this._item.CPTOSetPointCurrent.ValueStart > this._item.CPTOSetPointCurrent.ValueEnd)
                        {
                            if (this._item.CPTOSetPointCurrent.StepValue >= 0)
                            {
                                inputName.Status = AntdUI.TType.Error;
                                AntdUI.Message.warn(window, "阶跃值应为负值", autoClose: 3);
                                MessageBox.Show("阶跃值应为负值");
                                return;
                            }
                        }
                    }
                    else if (this.selectCPTOSetPointCurrent.SelectedIndex == 2)//脉冲方式
                    {
                        this._item.CPTOSetPointCurrent.SPCMode = 2;
                        this._item.CPTOSetPointCurrent.IsStep = false;
                        this._item.CPTOSetPointCurrent.ValueEnd = (int)inputNumberCPTOSetPointCurrentEnd.Value;
                        this._item.CPTOSetPointCurrent.StepValue = (int)inputNumberCPTOSetPointCurrentStep.Value;//起始脉冲时长
                        this._item.CPTOSetPointCurrent.StepKeep = (int)inputNumberCPTOSetPointCurrentKeep.Value;//峰值脉冲时长
                        this._item.CPTOSetPointCurrent.Repeat = (int)inputNumberPTOPulse.Value;//循环次数
                    }
                    else if (this.selectCPTOSetPointCurrent.SelectedIndex == 3)//KP方式
                    {
                        this._item.CPTOSetPointCurrent.SPCMode = 3;
                        this._item.CPTOSetPointCurrent.IsStep = true;
                        this._item.CPTOSetPointCurrent.ValueEnd = (int)inputNumberCPTOSetPointCurrentEnd.Value;
                        this._item.CPTOSetPointCurrent.StepValue = (int)inputNumberCPTOSetPointCurrentStep.Value;
                        this._item.CPTOSetPointCurrent.StepKeep = (int)inputNumberCPTOSetPointCurrentKeep.Value;

                        this._item.SetPointCurrentKP.KPStartKeep = (int)inputNumberKPStartStep.Value;//KP起步保持时间
                        this._item.SetPointCurrentKP.KPEndKeep = (int)inputNumberKPEndStep.Value;//KP峰值保持时间
                        this._item.SetPointCurrentKP.KPMaxOutSpeed = (float)inputNumberKPMaxTransSpeed.Value;//KP最大输出转速
                    }
                    else
                    {
                        this._item.CPTOSetPointCurrent.IsStep = false;
                        this._item.CPTOSetPointCurrent.ValueEnd = 0;
                        this._item.CPTOSetPointCurrent.StepValue = 0;
                        this._item.CPTOSetPointCurrent.StepKeep = 0;
                    }
                }
                else
                {
                    this._item.CPTOSetPointCurrent.IsStep = false;
                    this._item.CPTOSetPointCurrent.ValueStart = 0;
                    this._item.CPTOSetPointCurrent.ValueEnd = 0;
                    this._item.CPTOSetPointCurrent.StepValue = 0;
                    this._item.CPTOSetPointCurrent.StepKeep = 0;
                }

                this._item.ListRecordData = new List<string>();
                this._item.ListRecord = new List<RecordData>();
                for (int i = 0; i < _listRecord.Count; i++)
                {
                    var checkbox = Controls.Find($"Checkbox_{_listRecord[i].ID}", true).FirstOrDefault() as System.Windows.Forms.CheckBox;
                    if (checkbox != null && checkbox.Checked)
                    {
                        this._item.ListRecordData.Add(_listRecord[i].Name);
                        this._item.ListRecord.Add(_listRecord[i]);
                    }
                }

                if (!isnew)
                {
                    bool ret = DB.UpdateProject(this._item, MainWindow.configapp.LoginUser);
                    if (this._item.ListRecordData.Count > 0)
                    {
                        ret = DB.UpdateTemplateStepsRecord(this._item);
                    }
                    if (ret) MessageBox.Show("修改成功");
                    else
                        MessageBox.Show("修改未能成功写入数据库，请检查");
                }
                submit = true;
                this.Dispose();
            }
            catch (Exception)
            {
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            submit = false;
            this.Dispose();
        }

        private void selectSpeed_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            if (selectSpeed.SelectedIndex == 0)
            {
                this.inputNumberSpeedTo.Enabled = false;
                this.inputNumberSpeedPerSecond.Enabled = false;
                this.inputNumberSpeedPerStep.Enabled = false;
                this.inputNumberDriverPulse.Enabled = false;
            }
            else if (selectSpeed.SelectedIndex == 1)
            {
                this.inputNumberSpeedTo.Enabled = true;
                this.inputNumberSpeedPerSecond.Enabled = true;
                this.inputNumberSpeedPerStep.Enabled = true;
                this.inputNumberDriverPulse.Enabled = false;
            }
        }

        private void selectCFSetPointCurrent_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            this.panelKP.Visible = false;

            if (selectCFSetPointCurrent.SelectedIndex == 0)
            {
                this.inputNumberCFSetPointCurrentEnd.Enabled = false;
                this.inputNumberCFSetPointCurrentKeep.Enabled = false;
                this.inputNumberCFSetPointCurrentStep.Enabled = false;
                this.inputNumberCFPulse.Enabled = false;
            }
            else if (selectCFSetPointCurrent.SelectedIndex == 1)
            {
                this.inputNumberCFSetPointCurrentEnd.Enabled = true;
                this.inputNumberCFSetPointCurrentKeep.Enabled = true;
                this.inputNumberCFSetPointCurrentStep.Enabled = true;
                this.inputNumberCFPulse.Enabled = false;

                this.labelCFStep.Text = "阶跃梯度"; this.labelCFStepUnit.Text = "mA"; this.labelCFStepKeep.Text = "阶跃步长";
            }
            else if (selectCFSetPointCurrent.SelectedIndex == 2)
            {
                this.inputNumberCFSetPointCurrentEnd.Enabled = true;
                this.inputNumberCFSetPointCurrentKeep.Enabled = true;
                this.inputNumberCFSetPointCurrentStep.Enabled = true;
                this.inputNumberCFPulse.Enabled = true;

                this.labelCFStep.Text = "起始时长"; this.labelCFStepUnit.Text = "S"; this.labelCFStepKeep.Text = "峰值时长";
            }
            else if (selectCFSetPointCurrent.SelectedIndex == 3)
            {
                this.inputNumberCFSetPointCurrentEnd.Enabled = true;
                this.inputNumberCFSetPointCurrentKeep.Enabled = true;
                this.inputNumberCFSetPointCurrentStep.Enabled = true;
                this.inputNumberCFPulse.Enabled = false;
                this.panelKP.Visible = true;

                this.labelKP.Text = "CF 升阶（KP）测试必填项："; this.labelCFStep.Text = "阶跃梯度"; this.labelCFStepUnit.Text = "mA"; this.labelCFStepKeep.Text = "阶跃步长";
            }
        }

        private void selectCRSetPointCurrent_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            this.panelKP.Visible = false;
            if (selectCRSetPointCurrent.SelectedIndex == 0)
            {
                this.inputNumberCRSetPointCurrentEnd.Enabled = false;
                this.inputNumberCRSetPointCurrentKeep.Enabled = false;
                this.inputNumberCRSetPointCurrentStep.Enabled = false;
                this.inputNumberCRPulse.Enabled = false;
            }
            else if (selectCRSetPointCurrent.SelectedIndex == 1)
            {
                this.inputNumberCRSetPointCurrentEnd.Enabled = true;
                this.inputNumberCRSetPointCurrentKeep.Enabled = true;
                this.inputNumberCRSetPointCurrentStep.Enabled = true;
                this.inputNumberCRPulse.Enabled = false;

                this.labelCRStep.Text = "阶跃梯度"; this.labelCRStepUnit.Text = "mA"; this.labelCRStepKeep.Text = "阶跃步长";
            }
            else if (selectCRSetPointCurrent.SelectedIndex == 2)
            {
                this.inputNumberCRSetPointCurrentEnd.Enabled = true;
                this.inputNumberCRSetPointCurrentKeep.Enabled = true;
                this.inputNumberCRSetPointCurrentStep.Enabled = true;
                this.inputNumberCRPulse.Enabled = true;

                this.labelCRStep.Text = "起始时长"; this.labelCRStepUnit.Text = "S"; this.labelCRStepKeep.Text = "峰值时长";
            }
            else if (selectCRSetPointCurrent.SelectedIndex == 3)
            {
                this.inputNumberCRSetPointCurrentEnd.Enabled = true;
                this.inputNumberCRSetPointCurrentKeep.Enabled = true;
                this.inputNumberCRSetPointCurrentStep.Enabled = true;
                this.inputNumberCRPulse.Enabled = true;
                this.panelKP.Visible = true;

                this.labelKP.Text = "CR 升阶（KP）测试必填项："; this.labelCRStep.Text = "阶跃梯度"; this.labelCRStepUnit.Text = "mA"; this.labelCRStepKeep.Text = "阶跃步长";
            }
        }

        private void selectCPTOSetPointCurrent_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            this.panelKP.Visible = false;
            if (selectCPTOSetPointCurrent.SelectedIndex == 0)
            {
                this.inputNumberCPTOSetPointCurrentEnd.Enabled = false;
                this.inputNumberCPTOSetPointCurrentKeep.Enabled = false;
                this.inputNumberCPTOSetPointCurrentStep.Enabled = false;
                this.inputNumberPTOPulse.Enabled = false;
            }
            else if (selectCPTOSetPointCurrent.SelectedIndex == 1)
            {
                this.inputNumberCPTOSetPointCurrentEnd.Enabled = true;
                this.inputNumberCPTOSetPointCurrentKeep.Enabled = true;
                this.inputNumberCPTOSetPointCurrentStep.Enabled = true;
                this.inputNumberPTOPulse.Enabled = false;

                this.labelPTOStep.Text = "阶跃梯度"; this.labelPTOStepUnit.Text = "mA"; this.labelPTOStepKeep.Text = "阶跃步长";
            }
            else if (selectCPTOSetPointCurrent.SelectedIndex == 2)
            {
                this.inputNumberCPTOSetPointCurrentEnd.Enabled = true;
                this.inputNumberCPTOSetPointCurrentKeep.Enabled = true;
                this.inputNumberCPTOSetPointCurrentStep.Enabled = true;
                this.inputNumberPTOPulse.Enabled = true;

                this.labelPTOStep.Text = "起始时长"; this.labelPTOStepUnit.Text = "S"; this.labelPTOStepKeep.Text = "峰值时长";
            }
            else if (selectCPTOSetPointCurrent.SelectedIndex == 3)
            {
                this.inputNumberCPTOSetPointCurrentEnd.Enabled = true;
                this.inputNumberCPTOSetPointCurrentKeep.Enabled = true;
                this.inputNumberCPTOSetPointCurrentStep.Enabled = true;
                this.inputNumberPTOPulse.Enabled = true;
                this.panelKP.Visible = true;

                this.labelKP.Text = "PTO 升阶（KP）测试必填项："; this.labelPTOStep.Text = "阶跃梯度"; this.labelPTOStepUnit.Text = "mA"; this.labelPTOStepKeep.Text = "阶跃步长";
            }
        }

        private void selectCFState_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            if ((ClutchStatus)selectCFState.SelectedIndex != ClutchStatus.断开
                && (ClutchStatus)selectCRState.SelectedIndex != ClutchStatus.断开)
            {
                MessageBox.Show($"CF与CR不可同时吸合");
                this.selectCFState.SelectedIndex = 1;
            }
            if ((ClutchStatus)selectCFState.SelectedIndex == ClutchStatus.电流控制)
            {
                this.groupBoxCF.Enabled = true;
            }
            else
            {
                this.groupBoxCF.Enabled = false;
            }
        }

        private void selectCRState_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            if ((ClutchStatus)selectCFState.SelectedIndex != ClutchStatus.断开
                && (ClutchStatus)selectCRState.SelectedIndex != ClutchStatus.断开)
            {
                MessageBox.Show($"CF与CR不可同时吸合");
                this.selectCRState.SelectedIndex = 1;
            }
            if ((ClutchStatus)selectCRState.SelectedIndex == ClutchStatus.电流控制)
                this.groupBoxCR.Enabled = true;
            else
                this.groupBoxCR.Enabled = false;
        }

        private void inputNumberCFSetPointCurrentStart_ValueChanged(object sender, DecimalEventArgs e)
        {

        }

        private void selectCPTOState_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            if ((ClutchStatus)selectCPTOState.SelectedIndex == ClutchStatus.电流控制)
                this.groupBoxCPTO.Enabled = true;
            else
                this.groupBoxCPTO.Enabled = false;
        }

        private void buttonAddPara_Click(object sender, EventArgs e)
        {
            //var rp = this._item.ListRecord.First();
            var rp = new RecordData();
            var form = new RecordPara(rp);// { Size = new Size(500, 350) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {

            }
        }
    }
}
